package com.fy.fyspace.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * Web配置类
 * 用于配置消息转换器和其他Web相关配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 配置SSE消息转换器
     * 解决"No converter for [class com.fy.fyspace.common.result.Result] with preset Content-Type 'text/event-stream'"错误
     */
    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<>(converter.getSupportedMediaTypes());
        // 添加text/event-stream媒体类型支持
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        converter.setSupportedMediaTypes(supportedMediaTypes);
        return converter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customJackson2HttpMessageConverter());
    }
} 